﻿using System;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;

namespace GetInfoUser.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class UserService
    {
        [OperationContract]
        public void DoWork()
        {
            // Add your operation implementation here
            return;
        }

        [OperationContract]
        public UserInfo GetInfo(string servername, string user)
        {
            DirectoryEntry entry = new DirectoryEntry("LDAP://minhson.com");//,user,password);//(servername, user, password);
            // entry.Username = "may1";
            // entry.Password = "@ms123456";
            //object nativeObject = entry.NativeObject;
            UserInfo info = new UserInfo();
            try
            {
                DirectorySearcher ds = new DirectorySearcher(entry);
                ds.Filter = "(samaccountname=" + user + ")";
                //  ds.SearchScope = SearchScope.Subtree;
                SearchResult results = ds.FindOne();
                DirectoryEntry ac = (DirectoryEntry)results.GetDirectoryEntry();
                PrincipalContext ctx = new PrincipalContext(ContextType.Domain, servername);
                UserPrincipal u = UserPrincipal.FindByIdentity(ctx, user);
                
                DirectoryEntry deUser = u.GetUnderlyingObject() as DirectoryEntry;
                DirectoryEntry deUserContainer = deUser.Parent;
                var a = deUserContainer.Properties["distinguishedName"].Value;

               DateTime dt= Convert.ToDateTime( results.Properties["whencreated"][0].ToString());
                try
                {
                  //  info = new UserInfo(u.Name, results.Properties["streetaddress"][0].ToString(), u.EmailAddress, u.VoiceTelephoneNumber,dt);
                    info.ngay = Convert.ToDateTime(results.Properties["whencreated"][0].ToString()); ;
                    info.TLFullName = results.Properties["cn"][0].ToString();                                        
                }
                catch (Exception ex)
                {
                }
                try
                {
                    info.PHONE = results.Properties["telephonenumber"][0].ToString();                   
                }
                catch (Exception ex) { }
                try
                {                   
                    info.ADDRESS = results.Properties["streetaddress"][0].ToString();                    
                }
                catch (Exception ex) { }
                try
                {                                       
                    info.BrandName = results.Properties["physicaldeliveryofficename"][0].ToString();
                }
                catch (Exception ex) { }

                try
                {                   
                    info.BrandName = results.Properties["physicaldeliveryofficename"][0].ToString();
                }
                catch (Exception ex) { }
            }
            catch (DirectoryServicesCOMException ex)
            {
            }
            return info;
        }
        // Add more operations here and mark them with [OperationContract]
    }
    public class UserInfo
    {
       public string TLFullName;
       public string EMAIL;
       public string ADDRESS;
       public string PHONE;
       public string Group;
       public DateTime ngay;
       public string BrandName;
        public UserInfo()
        {
            TLFullName = "";
            EMAIL = "";
            ADDRESS = "";
            PHONE = "";
            Group = "";
        }
        
    }
}
